package com.shenmazong.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.entity.TbArticle;
import com.shenmazong.entity.TbCounter;
import com.shenmazong.entity.TbMessage;
import com.shenmazong.entity.TbStage;
import com.shenmazong.mapper.MessageMapper;
import com.shenmazong.mapper.StageMapper;
import com.shenmazong.mapper.TbArticleMapper;
import com.shenmazong.mapper.TbCounterMapper;
import com.shenmazong.utils.ResultResponse;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CounterGive {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private TbArticleMapper tbArticleMapper;

    @Resource
    private TbCounterMapper tbCounterMapper;



    @RabbitListener(queuesToDeclare = @Queue("givez"))
    public void rabbbitGive(String message){
        String[] split = message.split("\\:");
        TbArticle tbArticle = tbArticleMapper.selectById(split[0]);
        System.out.println("tbArticle = " + tbArticle);
        TbMessage tbMessage = new TbMessage();
        TbCounter tbCounter = tbCounterMapper.selectOne(
                new QueryWrapper<TbCounter>().lambda().eq(TbCounter::getArticleId, Long.valueOf(tbArticle.getArticleId()))
        );
        tbCounter.setArticleCount(tbCounter.getArticleCount()+1);
        tbCounterMapper.updateById(tbCounter);

        TbMessage mes = messageMapper.selectIsExeit(Long.valueOf(tbArticle.getArticleId()),Long.valueOf(split[1]));
        if (mes == null){
            tbMessage.setArticleId(tbArticle.getArticleId());
            tbMessage.setMyId(tbArticle.getIssuedId());
            tbMessage.setUserId(Long.valueOf(split[1]));
            messageMapper.insert(tbMessage);
        }

    }

}
